#pragma once

#include "wx/splitter.h"
#include "wx/treectrl.h"
#include "wx/progdlg.h"

#include <vector>
#include <string>

#include "mhcategory.h"

class CMediaHouseMainFrm: public wxFrame, public wxThreadHelper {

DECLARE_DYNAMIC_CLASS_NO_COPY(CMediaHouseMainFrm)

public:
	CMediaHouseMainFrm(wxFrame *frame, wxWindowID id, const wxString& title,
			const wxPoint& pos, const wxSize& size, const long type);

	virtual ~CMediaHouseMainFrm();

	wxTreeCtrl * GetNavigatorCtrl() const {
		return m_NavigatorCtrl;
	}

	void Init();

	void OnExit(wxCommandEvent& event);
	void OnThreadUpdate(wxThreadEvent& evt);
	void OnClose(wxCloseEvent& evt);
	void OnAddDir(wxCommandEvent& event);
	void OnProgressUpdate(wxThreadEvent& evt);

protected:
	virtual wxThread::ExitCode Entry();

	void CreateNavigatorPane(wxWindow * parent);
	void CreateContentPane(wxWindow * parent);
	void LoadNavigatorItems();
	void CreateMenus();

	void RefreshTags();
	void RefreshCategories();
	void RefreshChildCategories(const wxTreeItemId & parentId,
			wxInt32 parentCategoryId, const wxString & parentPath);

	void LoadTagsFromDB();
	void LoadCategoriesFromDB();

	void ScanItem(const wxString & path);

	wxTreeItemId FindOrCreateChildItem(const wxTreeItemId & parentId,
			const wxString & text, bool checkItemData = false);

	void UpdateProgress(wxInt32 progress, const wxString & msg = wxEmptyString);

	void RunMediaUpdater();
private:
	enum {
		WORKER_DO_LOAD_ITEMS,
		WORKER_DO_SCAN_ITEM,
	};

	wxInt32 m_WorkerTask;
	wxString m_ScanningPath;

	wxWindow *m_left, *m_right;
	wxWindow *m_top, *m_bottom;
	wxSplitterWindow* m_splitter, *m_ContentSplitter;

	wxTreeCtrl * m_NavigatorCtrl;

	wxCriticalSection m_DataLocker;

	std::vector<std::string> m_Tags;

	typedef hiberlite::bean_ptr<mediahouse::CMHCategory> PCategory;
	std::vector<PCategory> m_Categories;

	wxProgressDialog *m_dlgProgress;

	wxThread * m_pUpdaterThread;

	DECLARE_EVENT_TABLE()
};
